Skip to content

Feature/add enum validation for pageable#23518

Open
Picazsoo wants to merge 11 commits intoOpenAPITools:masterfrom
Picazsoo:feature/add-enum-validation-for-pageable
Open

Feature/add enum validation for pageable#23518
Picazsoo wants to merge 11 commits intoOpenAPITools:masterfrom
Picazsoo:feature/add-enum-validation-for-pageable

Conversation

@Picazsoo
Copy link
Copy Markdown
Contributor

@Picazsoo Picazsoo commented Apr 10, 2026

The idea behind this PR is the fact that currently when page, size, sort are substituted with Pageable, then the default values for page, size, sort are lost and Pageable defaults to global spring defaults (size = 10; page = 0). The native spring annotation @PageableDefault makes it possible to respect the defaults specified in the api spec. Native spring annotation @SortDefault.SortDefaults(...) on the other hand preserves the sort default value if it exists.

Another issue stemming from conversion of page, size, sort to Pageableis the loss of query param validations. This PR implements two custom validators (@ValidPageable and @ValidSort) that verify the validity of the passed values and fail if the values are out of spec. These validators should be easily replaceable with custom implementations via importMapping - this might be useful if one has some non-standard sort params and wants to handle the validation in a specific way.

So all together this PR makes sure that the convenience provided by conversion from separate query params page, size, sort to Pageable does not result in less strict contract in the generated api.

Some of the logic is extracted to separate class on purpose. Since I would like to implement this also in java-spring generator for feature parity.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request. - @karismann @Zomzog @andrewemery @4brunu @yutaka0m @stefankoppier @e5l @dennisameling @wing328

Summary by cubic

Adds enum-based sort and page/size limit validation for Pageable in kotlin-spring (spring-boot) by generating @ValidSort and @ValidPageable and applying them to paginated operations. Also adds @PageableDefault(page = 0, size = 20) to Pageable params when no explicit page/size are defined, with samples and CI updated.

  • New Features

    • New options generateSortValidation and generatePageableConstraintValidation (requires beanValidations=true and library=spring-boot) to generate ValidSort.kt and ValidPageable.kt in configPackage.
    • Detects paginated operations (x-spring-paginated: true or via autoXSpringPaginated) and adds:
      • @PageableDefault(page = 0, size = 20) when Pageable is injected without explicit page/size parameters.
      • @ValidSort(allowedValues=[...]) when sort has inline or $ref enum values.
      • @ValidPageable(maxSize=..., maxPage=...) from size/page parameter maximum values; skipped if not set.
    • Added templates kotlin-spring/validSort.mustache and kotlin-spring/validPageable.mustache, test spec petstore-sort-validation.yaml, generator tests (including page and size limit cases), and a sample at samples/server/petstore/kotlin-springboot-sort-validation; CI runs it.
  • Migration

    • Enable with generateSortValidation: "true" and/or generatePageableConstraintValidation: "true", plus beanValidations: "true" and library: spring-boot (optionally autoXSpringPaginated: "true").
    • ValidSort.kt and ValidPageable.kt are generated under your configPackage.
    • @PageableDefault(page = 0, size = 20) is now added automatically for paginated operations without explicit page/size.
    • To activate validation: define enum values on sort; set maximum on page and/or size.

Written for commit 67884e8. Summary will update on new commits.

@Picazsoo Picazsoo marked this pull request as ready for review April 14, 2026 17:31
@Picazsoo
Copy link
Copy Markdown
Contributor Author

Picazsoo commented Apr 14, 2026

@PageableDefault(page = 0, size = 20) is now added automatically for paginated operations without explicit page/size.

This is not true - the defaults are set only when they are specified in API. When not specified, they do not appear (and basically fall back to the spring defaults of page = 0, size = 10. But no annotation is needed for that). Right @cubic-dev-ai ?

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 37 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/server/petstore/kotlin-springboot-sort-validation/pom.xml">

<violation number="1" location="samples/server/petstore/kotlin-springboot-sort-validation/pom.xml:133">
P2: Validation runtime provider is missing in Maven config; only Bean Validation API is declared, so custom `@ValidSort` constraints may not execute.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

<!-- Bean Validation API support -->
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Validation runtime provider is missing in Maven config; only Bean Validation API is declared, so custom @ValidSort constraints may not execute.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/server/petstore/kotlin-springboot-sort-validation/pom.xml, line 133:

<comment>Validation runtime provider is missing in Maven config; only Bean Validation API is declared, so custom `@ValidSort` constraints may not execute.</comment>

<file context>
@@ -0,0 +1,148 @@
+        <!-- Bean Validation API support -->
+        <dependency>
+            <groupId>jakarta.validation</groupId>
+            <artifactId>jakarta.validation-api</artifactId>
+        </dependency>
+        <dependency>
</file context>
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant